// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  traceUser:true
})
const db = cloud.database()
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
cloud.init()
function analysis2(str) {
  let i3 = str.indexOf('、')
  let i4 = str.indexOf('：')
  let obj = {
    title:str.slice(i3+1,i4),
    content:str.slice(i4+1),
  }
  return obj
}
function analysis(index,value) {
  let i1 = value.indexOf(index)
  let i2 = value.indexOf(index+1)
  if (i1 != -1 && i2 != -1) {
    let str = value.slice(i1,i2)
    console.log(str)
    return [analysis2(str), ...analysis(index+1,value)]
  }
  if (i1 != -1 ) {
    let str = value.slice(i1,i2)
    console.log(str)
    return [str]
  }
}
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  // analysis(1,res.data) 
  let data = [{},{}]
  await db.collection('tf49').get().then((res )=> {
    // res.data 是一个包含集合中有权限访问的所有记录的数据，不超过 20 条
    // console.log(res.data)
    data = analysis(1,res.data[0].gua)
    
  })

  return {
    data,
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}